<template>
	<view class="container">
		<form v-if="!is_getPhone">
			<image class="logo" src="../../static/images/wechat.png" mode="widthFix"></image>
			<view class="title">微信授权页面</view>
			<view class="profile">为了给您提供更好的服务，请先授权</view>
			<!-- <button type="primary" :disabled="butnLoading" :loading="butnLoading" class="weui-btn mini-btn" @click="getLogin">确定授权
			</button> -->
			<button type="primary" :disabled="butnLoading" :loading="butnLoading" class="weui-btn mini-btn" open-type="getUserInfo" @getuserinfo="getLogin">确定授权
			</button>
			<button class="weui-btn2 mini-btn" open-type="redirect" @click="leaveOut">暂不授权</button>
		</form>
		<form v-else>
			<image class="logo" src="../../static/images/wechat.png" mode="widthFix"></image>
			<view class="title">微信登陆</view>
			<view class="profile">为了给您提供更好的服务，请先登录</view>
			<button type="primary" class="weui-btn mini-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">确定登录
			</button>
			<button class="weui-btn2 mini-btn" open-type="redirect" @click="leaveOut">暂不登陆</button>
			<view class="redelegation" v-if="is_getPhone" @click="redelegation">
				如果登录未成功，请点击这里重新授权!
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				butnLoading: false,
				showLogin: false,
				options: '',
				is_getPhone: false
			}
		},

		/**
		 * 生命周期函数--点击事件集合
		 */
		methods: {
			leaveOut() {
				var url = this.options.returnUrl ? this.options.returnUrl : 'pages/index/index'
				uni.reLaunch({
					url: '/'+url
				});
			},


			getLogin() {		
				let that = this
				// 除了H5页面其他都可以调用该函数
				/* #ifndef H5 */
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						if (loginRes.code) {
							// 获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
									// console.log( infoRes.userInfo)						
									var post_data = {
										wechat_code: loginRes.code,
										wechat_uinfo_nickname: infoRes.userInfo.nickName,
										wechat_uinfo_avatarUrl: infoRes.userInfo.avatarUrl,
									}		
									that.$http({
										url: 'Api/Login/get_wechat_uinfo',
										data: post_data,
										// method: 'post',
										hideLoading: false,
										success: res => {
											if(res.code == 1){
												uni.setStorageSync('session_key', res.data.session_key)
												uni.setStorageSync('user_id', res.data.user_id)
												uni.setStorageSync('token', res.data.token)
												that.is_getPhone = !that.is_getPhone
											}
										}
									})
									
									
								}
							});
						} else {
							uni.showToast({
								title: '登陆失败',
								icon: 'none',
								duration: 1500
							})
						}
					}
				})
				/* #endif */
			},
			
			
			
			
			getPhoneNumber(e){
				var returnUrl = this.options.returnUrl?this.options.returnUrl:'pages/index/index'
				if(e.detail.errMsg == 'getPhoneNumber:ok'){
					var that = this
					var post_data = {
						encrypteddata: e.detail.encryptedData,
						iv: e.detail.iv,
						sessionkey: uni.getStorageSync('session_key'),
						user_id: uni.getStorageSync('user_id')
					}
					
					that.$http({
						url: 'Api/Login/get_wechat_utell',
						data: post_data,
						hideLoading: false,
						success: res => {
							if(res.code == 1){
								uni.reLaunch({
									url: '/'+ returnUrl
								});
							}else{
								that.is_getPhone = false
							}
						}
					})
				}
			},
			
			
			check_key(){
				var that = this
				uni.checkSession({
					success() {
						console.log('session_key未过期')
						that.is_getPhone = true
					},
					fail() {
						console.log('session_key已过期')
						uni.removeStorageSync('session_key');
						that.is_getPhone = false
					}
				})
			},
			
			redelegation(){
				this.is_getPhone = false
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			this.options = options
			console.log("登录信息:")
			console.log(options)
			this.check_key()
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady() {

		},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh() {

		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom() {

		},
	}
</script>

<style scoped>
	form {
		width: 750rpx;
	}

	.logo {
		/* width:200rpx; */
		display: block;
		margin: 0 auto;
		width: 200rpx;
		margin-top: 180rpx;
		/* margin-top:180rpx;
    margin-left:275rpx; */
	}

	.title {
		text-align: center;
		margin-top: 70rpx;
	}

	.profile {
		text-align: center;
		margin-top: 30rpx;
		font-size: 14px;
		color: #888;
	}

	.mini-btn {
		margin-top: 50rpx;
		margin-left: 50px;
		margin-right: 50px;
	}

	.weui-btn2 {
		display: block;
		background: #ccc;
		color: #fff;
		padding-left: 14px;
		padding-right: 14px;
		box-sizing: border-box;
		font-size: 18px;
		text-align: center;
		text-decoration: none;
		border-radius: 5px;
	}
	
	.redelegation{
		text-align: center;
		margin-top: 50rpx;
		font-size: 24rpx;
		color: #909399;
	}
</style>
